package cn.growthgate.fgo.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.support.spring.FastJsonJsonView;

import cn.growthgate.fgo.util.RequestUtils;

/**
 * 错误Controller
 * 
 * @author GrowthGate
 * @since 2019年1月22日
 *
 */
@Controller
public class ErrorController implements org.springframework.boot.web.servlet.error.ErrorController {

	@RequestMapping("/error")
	public ModelAndView handleError(HttpServletRequest request) {
		// 获取statusCode
		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
		if (statusCode == 404) {
			if (RequestUtils.isAjaxRequest(request)) {
				// ajax请求
				return new ModelAndView(new FastJsonJsonView()).addObject("msg", "请求的地址不存在").addObject("code", 404);
			} else {
				// 非ajax请求，响应404页面
				return new ModelAndView("error/404");
			}
		} else {
			return new ModelAndView("error/500");
		}
	}

	@Override
	public String getErrorPath() {
		return "/error";
	}

}
